package org.dlust;

import cn.itcast.pojo.Country;

import cn.itcast.pojo.Province;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ConditionConfig {
  // 如果配置文件里配置了country属性，并于name和system的时候，才注入
  @ConditionalOnProperty(prefix = "country", name = {"name", "system"})
  @Bean
  public Country country(@Value("${country.name}") String name, @Value("${country.system}") String system) {
    Country country = new Country();
    country.setName(name);
    country.setSystem(system);
    return country;
  }

  @Bean
  // 如果ioc容器中不存在country，则注入Province，否则不注入
//  @ConditionalOnMissingBean(Country.class)
  // 如果当前环境中存在DispatcherServlet类，则注入Province，否则不注入
  // 如果当前环境中引入了Web的起步依赖，则有
    @ConditionalOnClass(name ="org.springframework.web.servlet.DispatcherServlet")
  public Province province() {
    return new Province();
  }
}
